#创建文件夹
def mkdir(path):
    import os
    isExists = os.path.exists(path)
    if not isExists:
        os.makedirs(path)


#引入url包，主要针对页面获取
from urllib import request
#引入BeautifulSoup 包， 主要针对格式化html
from bs4 import BeautifulSoup
import re
import time
import os

#保存的文件夹
path = r'C:\Users\Billy\Desktop\test\TencentClassRoom'

#域名地址
opurl = "https://ke.qq.com"
#原始页面
url = "https://ke.qq.com/course/list?mt=1001"
#获取页面内容
html = request.urlopen(url).read().decode('utf-8')
#格式化内容
soup = BeautifulSoup(html, 'html.parser')
#查找对应内容
titles = soup.find_all('a', "item-tt-link")

#循环获取具体标题标签中的内容
for title in titles:
    #处理当前标题（给标题增加目录）
    itclassPath = path + '\%s' % title.string.replace('/', '_').replace(
        '\t', '_')
    #创建目录
    mkdir(itclassPath)

    #该标题下链接地址
    detailurl = "https:" + title.get('href')
    # detailheaders = {
    #     ":authority":"ke.qq.com",
    #     ":method":"GET",
    #     ":path":detailurl.replace(opurl,''),
    #     ":scheme":"https",
    #     "accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
    #     "accept-encoding":"gzip, deflate, br",
    #     "accept-language":"zh-CN,zh;q=0.9",
    #     "cache-control":"max-age=0",
    #     #"referer":https://ke.qq.com/course/list?mt=1001,
    #     "upgrade-insecure-requests":"1",
    #     "user-agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36"
    #     }
    # detailheaders = {
    #     # ":authority":"ke.qq.com",
    #     # ":method":"GET",
    #     # ":path":detailurl.replace(opurl,''),
    #     # ":scheme":"https",
    #     "user-agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36"
    #     }
    # detailPageurl = request.Request(detailurl, headers=detailheaders)
    # #获取该标题下链接地址内容
    # detailHtml = request.urlopen(detailPageurl).read().decode('utf-8')
    # #格式化内容
    # detailsoup = BeautifulSoup(detailHtml,'html.parser')
    # #查找对应内容
    # detailVideo = detailsoup.find_all('div', "tabs-content hide")

    #备注文件地址
    remarkPath = itclassPath + "\\remark.txt"
    with open(remarkPath, "w+") as file:
        #写备注文件
        file.write(title.string + '\n')
        file.write(detailurl + '\n\n')

    # #目录文件地址
    # CatalogPath = itclassPath+"\\Catalog.txt"
    # for video in detailVideo:
    #     print(title)
    #     #写目录文件
    #     with open(CatalogPath, "w+") as file:
    #         file.write(title.string+'\n')
    #         file.write(detailurl+'\n\n')
